package com.bawei.search.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.search.databinding.ItemKeywordLayoutBinding

/**
 * Author: Jason Zhang
 * Date: 2025/2/5
 * Desc:
 */
class KeyWordListAdapter constructor(private val list: MutableList<String>):  RecyclerView.Adapter<KeyWordListAdapter.KeywordItemViewHolder>(){

    private var itemClickListener: OnItemClickListener? = null

    inner class KeywordItemViewHolder constructor(val dataBind: ItemKeywordLayoutBinding): RecyclerView.ViewHolder(dataBind.root)

    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KeywordItemViewHolder {
        return KeywordItemViewHolder(ItemKeywordLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: KeywordItemViewHolder, position: Int) {
        holder.dataBind.tvKeyword.text = list[position]
        holder.dataBind.tvKeyword.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }

    }

    /**
     * 点击事件接口类
     */
    interface OnItemClickListener {
        fun onItemChildClick(v: View?, position: Int)
    }


}